'use strict';

const Controller = require('egg').Controller,
  _ = require('lodash'),
  redisKey = require('../utils/redis_key'),
  utils = require('../utils/utils');

class OperatorController extends Controller {

  // 用户列表
  async list() {
    const body = this.ctx.request.query;
    const pageIndex = body.pageIndex || 1;
    const pageSize = body.pageSize || 20;
    const skip = pageSize * (pageIndex - 1);
    const data = await this.ctx.app.model.Operator.find({}, {
      create_time: 1,
      realname: 1,
      roles: 1,
      username: 1,
      update_time: 1,
    }).skip(skip).limit(parseInt(pageSize))
      .sort({create_time: -1});
    const total = await this.ctx.app.model.Operator.count({});

    this.ctx.body = {
      code: 0,
      data: {
        total,
        list: data,
        pageIndex,
      },
    };
  }

  // 查询单个用户
  async get() {
    this.ctx.validate({
      id: 'string',
    }, this.ctx.params);

    const data = await this.ctx.app.model.Operator.findOne({
      _id: this.ctx.params.id,
    }, {
      create_time: 1,
      realname: 1,
      roles: 1,
      username: 1,
      update_time: 1,
    });

    this.ctx.body = {
      code: 0,
      data,
      msg: '查询成功',
    };
  }

  // 删除用户
  async delete() {
    this.ctx.validate({
      id: 'string',
    }, this.ctx.params);

    const ret = await this.ctx.app.model.Operator.remove({
      id: this.ctx.params.id,
    });

    this.ctx.body = {
      code: 0,
      msg: '删除成功',
    };
  }

  // 更新用户
  async update() {
    this.ctx.validate({
      id: 'string',
    }, this.ctx.params);

    const body = this.ctx.request.body;
    body.update_time = new Date();
    const roles = [];
    if (body.idx) {
      switch (body.idx) {
        case 9:
          roles.push({
            idx: 9,
            role_name: '营销角色',
          });
          break;
        case 99:
          roles.push({
            idx: 99,
            role_name: '开发角色',
          });
        case 999:
          roles.push({
            idx: 999,
            role_name: '管理员',
          });
        default:
          break;
      }
      body.roles = roles;
    }

    const ret = await this.ctx.app.model.Operator.update({
      _id: this.ctx.params.id,
    }, body);

    this.ctx.body = {
      code: 0,
      msg: '更新成功',
    };
  }


  async login() {
    this.ctx.validate({
      username: 'string',
      password: 'string',
    });

    const body = this.ctx.request.body;
    const data = await this.ctx.service.operator.login({
      username: body.username,
      password: body.password,
    });

    if (data.error) {
      return this.ctx.body = {
        code: 1,
        msg: data.error,
      };
    }

    this.ctx.body = {
      code: 0,
      data: {
        token: data.token,
        idx: data.idx,
        username: body.username,
      },
      msg: '登录成功',
    };
  }

  async logout() {
    const ctx = this.ctx;
    try {
      this.ctx.validate({
        token: 'string',
      }, ctx.request.headers);
      const redisKeyToken = redisKey.token(ctx.request.headers.token);
      await ctx.app.redis.del(redisKeyToken);
      ctx.body = {
        code: 0,
        msg: '退出成功',
      };
    } catch (e) {
      ctx.body = {
        code: -1,
        msg: `${e.message}`,
      };
    }
  }

  async create() {
    this.ctx.validate({
      username: 'string',
      password: 'string',
      realname: 'string',
      idx: 'string',
    });

    const body = this.ctx.request.body;
    const data = await this.ctx.service.operator.create({
      username: body.username,
      password: body.password,
      realname: body.realname,
      idx: body.idx,
    });

    if (data.error) {
      return this.ctx.body = {
        code: 1,
        msg: data.error,
      };
    }

    this.ctx.body = {
      code: 0,
      msg: '操作成功',
    };
  }
}

module
  .exports = OperatorController;
